package com.seeyou.lake.base

import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.WindowInsetsController
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.seeyou.lake.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope

/**
 * @des 不依赖Mvvm的BaseActivity
 * @date 2021/9/23
 * @author fade
 */
abstract class EActivity<VB : ViewBinding> : AppCompatActivity(),
    CoroutineScope by MainScope() {

    protected val mViewBinding: VB by lazy { yourViewBing() }
    private var loadingView: StarLoadingView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.layout_base)
        setLightMode()
        findViewById<FrameLayout>(R.id.fl_base).addView(mViewBinding.root)
        loadingView = findViewById(R.id.loading_base)
        loadingView?.visibility = View.GONE
        init()
        setClick()
        onMyInternetListener()
    }

    /**
     * 设置状态栏字体-黑色
     */
    private fun setLightMode() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            val insetsController = window.insetsController
            insetsController?.setSystemBarsAppearance(
                WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS,
                WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
            )
        } else {
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        }
    }

    /**
     * 获取ViewBing
     */
    abstract fun yourViewBing(): VB

    /**
     * 初始化
     */
    abstract fun init()

    open fun onMyInternetListener() {

    }

    open fun setClick() {

    }

    fun showLoadingView() {
        loadingView?.visibility = View.VISIBLE
    }

    fun hideLoadingView() {
        loadingView?.visibility = View.GONE
    }

}